/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sms.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.sms.entity.StoreRechargeSmsLog;
import com.chlitina.store.modules.sms.service.StoreRechargeSmsLogService;
import com.chlitina.store.modules.sms.utils.SmsUtils;
import com.chlitina.store.modules.sys.entity.Settings;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.service.SettingsService;
import com.chlitina.store.modules.sys.service.SystemService;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 店面短信充值日志Controller
 * @author lvbo
 * @version 2013-07-12
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/sms/storeRechargeSmsLog")
public class StoreRechargeSmsLogController extends BaseController {

	@Autowired
	private StoreRechargeSmsLogService storeRechargeSmsLogService;
	
	@Autowired
	private SettingsService settingsService;
	
	@Autowired
	private SystemService systemService;
	
	
	@ModelAttribute
	public StoreRechargeSmsLog get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeRechargeSmsLogService.get(id);
		}else{
			return new StoreRechargeSmsLog();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(StoreRechargeSmsLog storeRechargeSmsLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		Store store = StoreUtils.getStore();
		if (store != null){
			storeRechargeSmsLog.setStore(store);
		}
        Page<StoreRechargeSmsLog> page = storeRechargeSmsLogService.find(new Page<StoreRechargeSmsLog>(request, response), storeRechargeSmsLog); 
        model.addAttribute("page", page);
		return "modules/sms/storeRechargeSmsLogList";
	}

	@RequestMapping(value = "form")
	public String form(StoreRechargeSmsLog storeRechargeSmsLog, Model model) {
		Store store = StoreUtils.getStore(true);
		if (store != null){
			storeRechargeSmsLog.setStore(store);
		}
		Settings smsRate = settingsService.getByKey(SmsUtils.SMS_RECHARGE_RATE);
		model.addAttribute("smsRate", smsRate.getIntValue());
		model.addAttribute("storeRechargeSmsLog", storeRechargeSmsLog);
		return "modules/sms/storeRechargeSmsLogForm";
	}

	@RequestMapping(value = "save")
	public String save(StoreRechargeSmsLog storeRechargeSmsLog, Model model, RedirectAttributes redirectAttributes) {
		storeRechargeSmsLog.setSmsRechargeTime(new Date());
		if (!beanValidator(model, storeRechargeSmsLog)){
			return form(storeRechargeSmsLog, model);
		}
		storeRechargeSmsLogService.save(storeRechargeSmsLog);
		Store store = StoreUtils.getStore(true);
		store.setAccount_balance(store.getAccount_balance() - storeRechargeSmsLog.getSmsRechargeMoney());
		store.setSms_remain_amount(store.getSms_remain_amount() + storeRechargeSmsLog.getSmsRechargeAmount());
		systemService.save(store);
		addMessage(redirectAttributes, storeRechargeSmsLog.getSmsRechargeAmount()+"条短信充值成功");
		return "redirect:"+Global.ADMIN_PATH+"/sms/storeRechargeSmsLog/form";
	}
	
}
